//
//  CollumnListViewController.m
//  PlayerFix
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 DAT. All rights reserved.
//

#import "CollumnListViewController.h"
#import "ColumnListTableViewCell.h"
#import "ColumnListModel.h"
#import "DataViewController.h"

@interface CollumnListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *myTableView;

@property (nonatomic, strong) NSMutableArray *allArray;


@end

@implementation CollumnListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor redColor];
    
    self.allArray = [NSMutableArray array];
    
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(325/2, 0, 50, 20)];
    nameLabel.text = _name;
    nameLabel.textColor = [UIColor whiteColor];
    self.navigationItem.titleView = nameLabel;
    
    // 左侧返回按钮
    UIImage *leftImage = [UIImage imageNamed:@"gobackBtn.png"];
    // 取消渲染
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375, 667 - 64) style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    
    [self addHeaderRefresh];

    
}

- (void)addHeaderRefresh
{
    self.myTableView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self getAfnModel];
    }];
    
    [self.myTableView.header beginRefreshing];
}

- (void)leftButtonAction:(UIBarButtonItem *)barButton{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)getAfnModel{

    [DATAsyncGetAFNBlock GetAFNURLWithString:_url andBody:nil andCookie:nil resultBlock:^(id result) {
        
        NSArray *bigArray = result;
        for (NSDictionary *dic in bigArray) {
            ColumnListModel *columnListModel = [[ColumnListModel alloc] init];
            [columnListModel setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:columnListModel];
        }
        
        [self.myTableView reloadData];
        [self.myTableView.header endRefreshing];
    } errorBlock:^(NSError *error) {
        [self.myTableView.header endRefreshing];
        NSLog(@"classListError = %@",error);
        
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier = @"columnListCell";
    ColumnListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[ColumnListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.columnListModel = [_allArray objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ColumnListModel *columnListModel = [self.allArray objectAtIndex:indexPath.row];
    DataViewController *dataVC = [[DataViewController alloc] init];
    dataVC.ID = columnListModel.id;
    [self.navigationController pushViewController:dataVC animated:YES];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
